﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Access.PowerTools.Settings;
using Access.PowerTools.AbstractionLayer;
using Access.PowerTools.Utilities;

namespace Access.PowerTools.TestConsole
{
    [TestFixture] 
    public class TestSet_0003_ExportObjects
    {
        [SetUp]
        public void Setup()
        {
            Access.PowerTools.Settings.Globals.Init();
            Access.PowerTools.Settings.Globals.SimulationModeOn = true;

            Access.PowerTools.Settings.Globals.HostApplication =
                new Access.PowerTools.Simulation.Engine.SimulationCommunicationBridge();
        }

        [TearDown]
        public void TearDown()
        {
        }

        [Test]
        public void Test_001_ExportFolderExists()
        {
            System.Console.WriteLine(Access.PowerTools.Settings.Globals.ExportFilesFolder);

            bool ret = System.IO.Directory.Exists(Access.PowerTools.Settings.Globals.ExportFilesFolder);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        private void clearBackUpFolder()
        {
            FilesAndFolders.ClearFolder(Access.PowerTools.Settings.Globals.ExportFilesFolder);
        }

        [Test]
        public void Test_002_ExportOneObject_Form()
        {
            clearBackUpFolder();

            string objectName = "Categories";
            Access.PowerTools.AbstractionLayer.AccessDataBaseObjectTypeEnumeration objectType =
                 Access.PowerTools.AbstractionLayer.AccessDataBaseObjectTypeEnumeration.Form;

            string backUpFileFullPath =
                Access.PowerTools.Settings.Globals.ExportFilesFolder +
                AbstractionLayer.AccessDatabaseObjectInfo.GetBackUpFileName(objectType,objectName);

            AccessApplication app = new AccessApplication(); 
            AccessDatabase database = app.CurrentDatabase;

            AccessDatabaseObjectInfo objectInfo = new
               AccessDatabaseObjectInfo(objectType, objectName);   

            Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent exportAgent
                = new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(app);
            exportAgent.SaveAsText(
                 objectType,
                 objectName,
                 backUpFileFullPath );

            string sourceText = System.IO.File.ReadAllText(objectInfo.SimulationSeedObjectInfoFileFullPath);
            string copiedText = System.IO.File.ReadAllText(backUpFileFullPath);
   
            bool ret = (string.Compare(sourceText, copiedText, true)==0);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_003_ExportOneObject_Table()
        {
            clearBackUpFolder();

            string objectName = "Categories";
            Access.PowerTools.AbstractionLayer.AccessDataBaseObjectTypeEnumeration objectType =
                 Access.PowerTools.AbstractionLayer.AccessDataBaseObjectTypeEnumeration.Table;

            string backUpFileFullPath =
                Access.PowerTools.Settings.Globals.ExportFilesFolder +
                AbstractionLayer.AccessDatabaseObjectInfo.GetBackUpFileName(objectType, objectName);

            AccessApplication app = new AccessApplication();
            AccessDatabase database = app.CurrentDatabase;

            AccessDatabaseObjectInfo objectInfo = new
               AccessDatabaseObjectInfo(objectType, objectName);

            Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent exportAgent
                = new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(app);
            exportAgent.SaveAsText(
                 objectType,
                 objectName,
                 backUpFileFullPath);

            string sourceText = System.IO.File.ReadAllText(objectInfo.SimulationSeedObjectInfoFileFullPath);
            string copiedText = System.IO.File.ReadAllText(backUpFileFullPath);

            string sourceText1 = System.IO.File.ReadAllText(objectInfo.SimulationSeedObjectSchemaFileFullPath);
            //string sourceText1 = System.IO.File.ReadAllText(objectInfo.SimulationSeedObjectInfoFileFullPath);
            string copiedText1 = System.IO.File.ReadAllText(Access.PowerTools.Settings.Globals.ExportFilesFolder +
                                  System.IO.Path.GetFileName(objectInfo.SimulationSeedObjectSchemaFileFullPath)) ;

            bool ret = (
                string.Compare(sourceText, copiedText, true) == 0 &&
                string.Compare(sourceText1, copiedText1, true) == 0
                );

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_004_ClearBackUpFolder()
        {
            clearBackUpFolder();

            string[] files = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.ExportFilesFolder);

            bool ret = (files == null || files.Length == 0);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");


        }

        [Test]
        public void Test_005_ExportAllObjects()
        {
            clearBackUpFolder();

            bool ret = false;

            AccessApplication app = new AccessApplication();
            AccessDatabase database = app.CurrentDatabase;
            Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent agent =
                new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(app);

            //foreach (AccessDatabaseObjectInfo objectInfo in
            //          database.AllTables.Values)
            foreach (AccessDatabaseObjectInfo objectInfo in
                      database.AllObjects )
            {
                string sourceFileFullPath =
                    objectInfo.SimulationSeedObjectInfoFileFullPath;  

                string backUpFileFullPath =
                    Access.PowerTools.Settings.Globals.ExportFilesFolder +
                    AbstractionLayer.AccessDatabaseObjectInfo.GetBackUpFileName(
                       objectInfo.Type, objectInfo.Name);

                //System.IO.File.Copy(sourceFileFullPath,
                //                    backUpFileFullPath,
                //                    true);
                agent.SaveAsText(objectInfo.Type, objectInfo.Name, backUpFileFullPath);  

                string sourceText = System.IO.File.ReadAllText(objectInfo.SimulationSeedObjectInfoFileFullPath);
                string copiedText = System.IO.File.ReadAllText(backUpFileFullPath);

                string sourceText1 = "";
                string copiedText1 = "";

                if (objectInfo.Type == AccessDataBaseObjectTypeEnumeration.Table)
                {
                    sourceText1 = System.IO.File.ReadAllText(objectInfo.SimulationSeedObjectSchemaFileFullPath);
                    copiedText1 = System.IO.File.ReadAllText(Access.PowerTools.Settings.Globals.ExportFilesFolder +
                                          System.IO.Path.GetFileName(objectInfo.SimulationSeedObjectSchemaFileFullPath));
                }

                ret = (
                    string.Compare(sourceText, copiedText, true) == 0 &&
                    string.Compare(sourceText1, copiedText1, true) == 0
                    );

                if (!ret)
                {
                    System.Console.WriteLine("{0}.{1} - object backup failed", objectInfo.Type, objectInfo.Name    );
                    break;  
                }
            }
            
            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_006_BackUpDatabase()
        {
            clearBackUpFolder();

            AccessApplication app = new AccessApplication();
            AccessDatabase database = app.CurrentDatabase;
            Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent agent =
                new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(app);
            agent.BackUpDatabase(Access.PowerTools.Settings.Globals.ExportFilesFolder);

            string[] files1 = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.ExportFilesFolder);
            string[] files2 = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder);

            Console.WriteLine("{0} - {1}", files1.Length, files2.Length); 
            bool ret = (files1.Length == files2.Length);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }


        [Test]
        public void Test_007_BackUpDatabase()
        {

            bool ret1 = false;

            clearBackUpFolder();

            AccessApplication app = new AccessApplication();
            AccessDatabase database = app.CurrentDatabase;
            Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent agent =
                new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(app);
            agent.BackUpDatabase(Access.PowerTools.Settings.Globals.ExportFilesFolder);

            string[] files1 = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.ExportFilesFolder);
            string[] files2 = System.IO.Directory.GetFiles(Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder);

            foreach (string file1 in files1)
            {
                string fileName1 = System.IO.Path.GetFileName(file1);
                foreach (string file2 in files2)
                {
                    string fileName2 = System.IO.Path.GetFileName(file2);
                    if (fileName1 == fileName2)
                    {
                        string sourceText = System.IO.File.ReadAllText(file1);
                        string copiedText = System.IO.File.ReadAllText(file2);

                        ret1 = (string.Compare(sourceText, copiedText, true) == 0);
                        if (!ret1)
                        {
                            System.Console.WriteLine("'{0}' - invalid copy", fileName1);   
                            break;
                        }
                    }
                }
                if (!ret1) break;
            }

            bool ret = ((files1.Length == files2.Length) && ret1);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

    }
}
